package com.hanrui.android.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hanrui.android.R;
import com.hanrui.android.adapter.BillHelpAdapter;
import com.hanrui.android.library.base.ActionBarBaseActivity;
import com.hanrui.android.model.InvoiceListBean;
import com.hanrui.android.net.contract.mine.BillHelpContract;
import com.hanrui.android.net.presenter.mine.BillHelpPresenter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 发票助手
 */
public class BillHelperActivity extends ActionBarBaseActivity<BillHelpPresenter> implements BillHelpContract.View, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @Inject
    BillHelpAdapter adapter;

    View addBillFootView;

    @Override
    protected void initInjector() {mActivityComponent.inject(this);}


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview_base);

        unbinder = ButterKnife.bind(this);

        initView();
    }

    private void initView() {

        mTitleBar.setTitle("发票助手");

        setIsPullRefresh(true);


        setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refresh(BillHelperActivity.this);
            }
        });

        addBillFootView = getLayoutInflater().inflate(R.layout.item_billhelper_foot, (ViewGroup) mRecyclerView.getParent(), false);

        addBillFootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AddBillActivity.start(BillHelperActivity.this,null);

            }
        });


        adapter.setEnableLoadMore(false);
        adapter.setOnItemClickListener(this);
        adapter.setFooterView(addBillFootView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(adapter);

    }

    @Override
    public void loadData(List<InvoiceListBean> dataList, int loadType) {
        setLoadDataResult(adapter,dataList,loadType,false);
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        InvoiceListBean bean = (InvoiceListBean) adapter.getItem(position);
        BillDetailActivity.start(BillHelperActivity.this, bean.getId());

    }


    @Override
    protected void onResume() {
        super.onResume();

        setRefreshing();
        mPresenter.refresh(this);

    }

    public static void start(Context context){

        Intent intent = new Intent(context,BillHelperActivity.class);
        context.startActivity(intent);

    }

}
